// Copyright 2011 Google Inc. All Rights Reseved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


package com.google.testing.testify.risk.frontend.model;

import java.io.Serializable;

import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

/**
 * JDO object for a Label.  Labels can be assigned to any part of an ACC model.
 * A label is stored as a two-state label (essentially, a name and value pair).  This can
 * represent things like:
 *   "Priority-P4" where P4 is the value to the name Priority.
 * 
 * For labels where there is not a name/value structure, the label is simply stored as the name.
 *   "Security", for example, would have an empty value and the entire label text in name.
 *
 * The entire label is not stored as one text field, but can be generated by this class using
 * the function getLabelText().  This function should be used instead of doing this manually.
 * 
 * @author jimr@google.com (Jim Reardon)
 */
@PersistenceCapable(detachable = "true")
public class AccLabel implements Serializable {
  @PrimaryKey
  @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
  @Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
  private String id;

  @Persistent
  private Long projectId;
  
  @Persistent
  private AccElementType elementType;

  @Persistent
  private Long elementId;

  @Persistent
  private String name;
  
  @Persistent
  private String value;

  public String getId() {
    return id;
  }

  public void setId(String id) {
    this.id = id;
  }

  public Long getProjectId() {
    return projectId;
  }

  public void setProjectId(Long projectId) {
    this.projectId = projectId;
  }

  public AccElementType getElementType() {
    return elementType;
  }

  public void setElementType(AccElementType elementType) {
    this.elementType = elementType;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getValue() {
    return value;
  }

  public void setValue(String value) {
    this.value = value;
  }

  /**
   * Will attempt to split a label into its name / value pair, or set it all as name if it is not
   * a two-state label.
   */
  public void setLabelText(String labelText) {
    if (labelText == null) {
      name = null;
      value = null;
    } else {
      String[] split = labelText.split("-", 2);
      if (split.length > 0) {
        name = split[0];
        if (split.length > 1) {
          value = split[1];
        }
      }
    }
  }

  /**
   * Returns a combined label with the name and value in one string. Eg:
   *  name=Priority value=P4 would become "Priority-P4".
   *  name=Security value=null would become "Security".
   * @return the combined label.
   */
  public String getLabelText() {
    String labelText = name;
    if (value != null) {
      labelText += "-" + value;
    }
    return labelText;
  }

  public void setElementId(Long elementId) {
    this.elementId = elementId;
  }

  public Long getElementId() {
    return elementId;
  }
}
